//
//  VersionUpdatePopViewController.swift
//  ManBetX
//
//  Created by Cocoa on 2020/4/29.
//  Copyright © 2020 Fullen. All rights reserved.
//

import UIKit

/**
 * 版本更新功能 - 弹框
 */

// 立即更新点击回调
typealias VersionButtonClickBlock = () -> Void

class VersionUpdatePopViewController: BasePopViewController {

    enum CaseType {
        case optionalUpdate// 可选更新
        case requireUpdate // 要求更新
    }
    
    // IBOutlet
    @IBOutlet weak var container: UIView!// 信息界面
    // 信息界面底部距离底部间距
    @IBOutlet weak var containerBottomConstraint: NSLayoutConstraint!
    @IBOutlet weak var bottomView: UIView!// 底层View
    @IBOutlet weak var requireButton:UIButton!// 强制更新Button
    
    @IBOutlet weak var updateTitle: UILabel!// 标题信息
    @IBOutlet weak var updateInfo: UILabel!// 内容信息
    @IBOutlet weak var optionalView: UIView!// 可选更新父视图
    
    // Property
    var updateBlock: VersionButtonClickBlock?
    var cancelBlock: VersionButtonClickBlock?
    var caseType: CaseType = .optionalUpdate
    /*
     测试：
    
      VersionUpdatePopViewController.showVersionUpdatePopViewController {
              print("show")
          }
    */
    // MARK:--便捷展示
    class func showVersionUpdatePopViewController(
        caseType: CaseType,
        updateBlock: @escaping VersionButtonClickBlock,
        cancelBlock: @escaping VersionButtonClickBlock) {
        
        let popVC = VersionUpdatePopViewController.instanceFromStoryboard()!
        popVC.updateBlock = updateBlock
        popVC.cancelBlock = cancelBlock
        popVC.caseType = caseType
        popVC.showHightLevel()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.layoutIfNeeded()
        self.view.backgroundColor = UIColor.clear
        
        configrueUI()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        self.configureContainerHidden(hidden: false)
    }
    
    // 配置UI
    func configrueUI() {
        if self.caseType == .optionalUpdate {
            self.updateTitle.text = "您当前的版本已经太旧啦"
            self.updateInfo.text = "请立即升级为最新版本"
            self.requireButton.isHidden = true
            self.optionalView.isHidden = false
            self.configureBottomView()
        }else {
            self.updateTitle.text = "新版本已上线"
            self.updateInfo.text = "立即更新，带来最优体验"
            self.requireButton.isHidden = false
            self.optionalView.isHidden = true
        }
    }
    
    // 背景图点击关闭 -- 注意，只有可选更新时才配置
    func configureBottomView() {
        self.bottomView.whenTapped {[weak self] (gestureRecognizer) in
            
            self?.configureContainerHidden(hidden: true)
            if self?.cancelBlock != nil {
                self?.cancelBlock!()
            }
        }
    }
    // 控制视图展示与消失
    func configureContainerHidden(hidden:Bool) {
        if hidden == true {
            UIView.animate(withDuration: 0.2, animations: {
                self.containerBottomConstraint.constant = -self.container.bounds.size.height
                self.view.layoutIfNeeded()
            }) { (finished) in
                self.dismiss()
            }
        }else {
            UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 1.5, options: .curveEaseInOut
                , animations: {
                    let bottomConstant = (self.view.bounds.size.height/2 - self.container.bounds.size.height/2)
                    self.containerBottomConstraint.constant = bottomConstant
                    self.view.layoutIfNeeded()
            }, completion: nil)
        }
    }
    
    // 强制更新点击
    @IBAction func updateButtonClick(_ sender: UIButton) {
        
        if self.caseType == .optionalUpdate {
            self.configureContainerHidden(hidden: true)
        }
        
        if self.updateBlock != nil {
            self.updateBlock!()
        }
    }
    
    // 拒绝更新点击
    @IBAction func cancelButtonClick(_ sender: UIButton) {
        
        self.configureContainerHidden(hidden: true)
        
        if self.cancelBlock != nil {
            self.cancelBlock!()
        }
    }
    
    
}
